package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Taskscore;
import cn.sunlelin.educationserver.service.TaskscoreService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.jws.HandlerChain;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class TaskscoreController {
    @Autowired
    private TaskscoreService taskscoreService;

    @PostMapping("/taskscore/createTaskscore")
    public Map<String,Object> createTaskscore(@RequestBody Taskscore taskscore){
        Map<String,Object> map=new HashMap<>();
        if(taskscoreService.createTaskscore(taskscore)){
            map.put("code",200);
            map.put("msg","创建作业情况成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","创建作业情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/taskscore/updateTaskscore")
    public Map<String,Object> updateTaskscore(@RequestBody Taskscore taskscore){
        Map<String,Object> map=new HashMap<>();
        if(taskscoreService.updateTaskscore(taskscore)){
            map.put("code",200);
            map.put("msg","修改作业情况成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","修改作业情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @PostMapping("/user/taskscore/TaskscoreScoring")
    public Map<String,Object> TaskscoreScoring(@RequestBody Taskscore taskscore,
                                               @RequestHeader("userId") Integer userId){
        Map<String,Object> map=new HashMap<>();
        if(taskscoreService.TaskscoreScoring(taskscore,userId)){
            map.put("code",200);
            map.put("msg","批改作业情况成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","批改作业情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/taskscore/selectTaskscoreById/{taskscoreId}")
    public Map<String,Object> selectTaskscoreById(@PathVariable Integer taskscoreId){
        Taskscore taskscore=taskscoreService.selectTaskscoreById(taskscoreId);
        Map<String,Object> map=new HashMap<>();
        if(taskscore!=null){
            map.put("code",200);
            map.put("msg","根据Id查询作业情况成功");
            map.put("dataobject",taskscore);
        }
        else {
            map.put("code",500);
            map.put("msg","根据Id查询作业情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/taskscore/selectTaskscoreByUserId/{userId}")
    public Map<String,Object> selectTaskscoreByUserId(@PathVariable Integer userId){
        List<Taskscore> taskscoreList=taskscoreService.selectTaskscoreByUserId(userId);
        Map<String,Object> map=new HashMap<>();
        if(taskscoreList!=null){
            map.put("code",200);
            map.put("msg","根据用户Id查询作业情况成功");
            map.put("dataobject",taskscoreList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据用户Id查询作业情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/taskscore/selectTaskscoreByTaskId/{taskId}")
    public Map<String,Object> selectTaskscoreByTaskId(@PathVariable Integer taskId){
        List<Map<String,Object>> taskscoreList=taskscoreService.selectTaskscoreByTaskId(taskId);
        Map<String,Object> map=new HashMap<>();
        if(taskscoreList!=null){
            map.put("code",200);
            map.put("msg","根据作业Id查询作业情况成功");
            map.put("dataobject",taskscoreList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据作业Id查询作业情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/taskscore/getPageScoreByTaskId/{currentPage}/{pageSize}/{taskId}")
    public Map<String,Object> getPageScoreByTaskId(@PathVariable Integer currentPage,
                                            @PathVariable Integer pageSize,
                                            @PathVariable Integer taskId){
        if(currentPage==null) currentPage=1;
        if(pageSize==null)pageSize=2;
        Page<Map<String,Object>> page= taskscoreService.getPageScoreByTaskId(currentPage,pageSize,taskId);
        List<Map<String,Object>> pageList=page.getResult();
        long total = page.getTotal();
        Map<String,Object> m=new HashMap<>();
        m.put("pageList",pageList);
        m.put("total",total);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","根据作业Id分页查询作业情况成功");
        map.put("dataobject",m);
        return map;
    }

    @GetMapping("/taskscore/selectTaskscoreByUserAndTask/{userId}/{taskId}")
    public Map<String,Object> selectTaskscoreByUserAndTask(@PathVariable Integer userId,
                                                           @PathVariable Integer taskId){
        Map<String,Object> taskscoreOut=taskscoreService.selectTaskscoreByUserAndTask(userId,taskId);
        Map<String,Object> map=new HashMap<>();
        if(taskscoreOut!=null){
            map.put("code",200);
            map.put("msg","根据用户Id和作业Id查询作业情况成功");
            map.put("dataobject",taskscoreOut);
        }
        else {
            map.put("code",500);
            map.put("msg","根据用户Id和作业Id查询作业情况失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/taskscore/deleteTaskscore/{taskscoreId}")
    public Map<String,Object> deleteTaskscore(@PathVariable Integer taskscoreId){
        Map<String,Object> map=new HashMap<>();
        if(taskscoreService.deleteTaskscore(taskscoreId)){
            map.put("code",200);
            map.put("msg","删除作业情况成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除作业情况失败，此Id不存在");
            map.put("dataobject",null);
        }
        return map;
    }
}
